﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSL_Tools.XMLTools;
using System.Xml;

namespace ReportingAppConfig
{
    public class SingleRunReportBuilder
    {
        #region -- Public Properties ----------------------------------------------------
        public List<ReportSection> reportSections { get; private set; }
        #endregion

        #region -- Private Properties ---------------------------------------------------
        #endregion

        #region -- Constructors ---------------------------------------------------------
        public SingleRunReportBuilder() 
        {
            reportSections = new List<ReportSection>();
        }
        #endregion

        #region -- Public Methods -------------------------------------------------------
        public void GetConfigValues(XmlDocument xDoc)
        {
            foreach (XmlNode xNode in xDoc.SelectNodes("Configuration/SingleRunReportBuilder/ReportSection[@enabled = \"true\"]"))
            {
                ReportSection section = new ReportSection();
                section.GetConfigValues(xNode);
                reportSections.Add(section);
            }
        }

        public void SaveConfigValues(XmlDocument xDoc)
        {
        }

        #endregion

        #region -- Private Methods ------------------------------------------------------
        #endregion
    }

    public class RunComparisonReportBuilder
    {
        #region -- Public Properties ----------------------------------------------------
        public List<ReportSection> reportSections { get; private set; }
        #endregion

        #region -- Private Properties ---------------------------------------------------
        #endregion

        #region -- Constructors ---------------------------------------------------------
        public RunComparisonReportBuilder()
        {
            reportSections = new List<ReportSection>();
        }
        #endregion

        #region -- Public Methods -------------------------------------------------------
        public void GetConfigValues(XmlDocument xDoc)
        {
            foreach (XmlNode xNode in xDoc.SelectNodes("Configuration/RunComparisonReportBuilder/ReportSection[@enabled = \"true\"]"))
            {
                ReportSection section = new ReportSection();
                section.GetConfigValues(xNode);
                reportSections.Add(section);
            }
        }

        public void SaveConfigValues(XmlDocument xDoc)
        {
        }

        #endregion

        #region -- Private Methods ------------------------------------------------------
        #endregion
    }

    public class ReportSection
    {
        #region -- Public Properties ----------------------------------------------------
        public string sectionName { get; private set; }
        public string sectionTitle { get; private set; }
        public string chartFormatPropertySet { get; private set; }
        public string tableFormatPropertySet { get; private set; }
        public int topQty { get; private set; }
        public string sortOrder { get; private set; }

        public List<CoreChart> listOfCharts { get; private set; }
        public List<CoreTable> listOfTables { get; private set; }
        #endregion

        #region -- Private Properties ---------------------------------------------------
        #endregion

        #region -- Constructors ---------------------------------------------------------
        public ReportSection()
        {
            listOfCharts = new List<CoreChart>();
            listOfTables = new List<CoreTable>();
        }
        #endregion

        #region -- Public Methods -------------------------------------------------------
        /// <summary>
        /// Used to load the config user settings into the  <see cref="AppConfiguration"/> class settings
        /// </summary>
        public void GetConfigValues(XmlNode xNode)
        {
            sectionName = XmlHelper.GetXmlAttributeValue(xNode, "name", "");
            sectionTitle = XmlHelper.GetXmlAttributeValue(xNode, "title", "");
            chartFormatPropertySet = XmlHelper.GetXmlAttributeValue(xNode, "ChartFormatPropertySet", "");
            tableFormatPropertySet = XmlHelper.GetXmlAttributeValue(xNode, "TableFormatPropertySet", "");
            sortOrder = XmlHelper.GetXmlAttributeValue(xNode, "sortOrder", "DESC");
            topQty = XmlHelper.GetXmlAttributeValue(xNode, "topQty", 10);

            foreach (XmlNode xChild in xNode.SelectNodes("chart[@enabled = \"true\"]"))
            {
                CoreChart _chart = new CoreChart();
                _chart.name = XmlHelper.GetXmlAttributeValue(xChild, "name", "");
                _chart.chartTitle = XmlHelper.GetXmlAttributeValue(xChild, "chartTitle", _chart.name);
                _chart.chartTemplate = XmlHelper.GetXmlAttributeValue(xChild, "chartTemplate", "");
                _chart.chartFormatPropertySet = XmlHelper.GetXmlAttributeValue(xChild, "chartFormatPropertySet", chartFormatPropertySet);
                _chart.GetConfigValues(xChild);
                listOfCharts.Add(_chart);
            }
            foreach (XmlNode xChild in xNode.SelectNodes("table[@enabled = \"true\"]"))
            {
                //<table type="PerfmonIntervalTable" name="Processor Utilization" category="Processor" 
                //Counter="% Processor Time" Instance="_Total" Machine="__SUTList" enabled="true" />
                CoreTable _table = new CoreTable();
                _table.name = XmlHelper.GetXmlAttributeValue(xChild, "name", "");
                _table.category = XmlHelper.GetXmlAttributeValue(xChild, "category", "");
                _table.counter = XmlHelper.GetXmlAttributeValue(xChild, "Counter", "");
                _table.instance = XmlHelper.GetXmlAttributeValue(xChild, "Instance", "");
                _table.machine = XmlHelper.GetXmlAttributeValue(xChild, "Machine", "");
                _table.type = XmlHelper.GetXmlAttributeValue(xChild, "type", "");
                listOfTables.Add(_table);
            }
        }

        /// <summary>
        /// Used to set the <see cref="AppConfiguration"/> class settings back into the config user settings file and then to save them.
        /// </summary>
        public void SaveConfigValues(XmlDocument xDoc)
        {
        }

        #endregion

        #region -- Private Methods ------------------------------------------------------
        #endregion

    }
}
